home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / bash_114.zip / bash-1.14.2 / builtins / fc.def < prev    next >
Encoding:
Text File  |  1994-06-26  |  15.9 KB  |  688 lines

  1. This file is fc.def, from which is created fc.c.
  2. It implements the builtin "fc" in Bash.
  3.  
  4. Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6. This file is part of GNU Bash, the Bourne Again SHell.
  7.  
  8. Bash is free software; you can redistribute it and/or modify it under
  9. the terms of the GNU General Public License as published by the Free
  10. Software Foundation; either version 1, or (at your option) any later
  11. version.
  12.  
  13. Bash is distributed in the hope that it will be useful, but WITHOUT ANY
  14. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License along
  19. with Bash; see the file COPYING.  If not, write to the Free Software
  20. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. $PRODUCES fc.c
  23.  
  24. $BUILTIN fc
  25. $FUNCTION fc_builtin
  26. $DEPENDS_ON HISTORY
  27. $SHORT_DOC fc [-e ename] [-nlr] [first] [last] or fc -s [pat=rep] [cmd]
  28.  
  29. FIRST and LAST can be numbers specifying the range, or FIRST can be a
  30. string, which means the most recent command beginning with that
  31. string.
  32.  
  33.    -e ENAME selects which editor to use.  Default is FCEDIT, then EDITOR,
  34.       then the editor which corresponds to the current readline editing
  35.       mode, then vi.
  36.  
  37.    -l means list lines instead of editing.
  38.    -n means no line numbers listed.
  39.    -r means reverse the order of the lines (making it newest listed first).
  40.  
  41. With the `fc -s [pat=rep ...] [command]' format, the command is
  42. re-executed after the substitution OLD=NEW is performed.
  43.  
  44. A useful alias to use with this is r='fc -s', so that typing `r cc'
  45. runs the last command beginning with `cc' and typing `r' re-executes
  46. the last command.
  47. $END
  48.  
  49. #include <stdio.h>
  50. #include "../bashansi.h"
  51. #include "../shell.h"
  52. #if defined (HISTORY)
  53. #include <sys/param.h>
  54. #include <sys/types.h>
  55. #include <sys/stat.h>
  56. #include <sys/file.h>
  57. #include <errno.h>
  58. #include "../builtins.h"
  59. #include "../flags.h"
  60. #include "../maxpath.h"
  61. #include "../bashhist.h"
  62. #include <readline/history.h>
  63. #include "bashgetopt.h"
  64.  
  65. /* Not all systems declare ERRNO in errno.h... and some systems #define it! */
  66. #if !defined (errno)
  67. extern int errno;
  68. #endif /* !errno */
  69.  
  70. extern int echo_input_at_read;
  71.  
  72. extern int unlink ();
  73.  
  74. /* **************************************************************** */
  75. /*                                    */
  76. /*    The K*rn shell style fc command (Fix Command)            */
  77. /*                                    */
  78. /* **************************************************************** */
  79.  
  80. /* fc builtin command (fix command) for Bash for those who
  81.    like K*rn-style history better than csh-style.
  82.  
  83.      fc [-e ename] [-nlr] [first] [last]
  84.  
  85.    FIRST and LAST can be numbers specifying the range, or FIRST can be
  86.    a string, which means the most recent command beginning with that
  87.    string.
  88.  
  89.    -e ENAME selects which editor to use.  Default is FCEDIT, then EDITOR,
  90.       then the editor which corresponds to the current readline editing
  91.       mode, then vi.
  92.  
  93.    -l means list lines instead of editing.
  94.    -n means no line numbers listed.
  95.    -r means reverse the order of the lines (making it newest listed first).
  96.  
  97.      fc -e - [pat=rep ...] [command]
  98.      fc -s [pat=rep ...] [command]
  99.  
  100.    Equivalent to !command:sg/pat/rep execpt there can be multiple PAT=REP's.
  101. */
  102.  
  103. static char *fc_dosubs (), *fc_replace (), *fc_gethist (), *fc_readline ();
  104. static int fc_gethnum ();
  105. static void fc_replhist (), fc_addhist ();
  106.  
  107. /* Data structure describing a list of global replacements to perform. */
  108. typedef struct repl {
  109.   struct repl *next;
  110.   char *pat;
  111.   char *rep;
  112. } REPL;
  113.  
  114. #define USAGE    "usage: fc [-e ename] [-nlr] [first] [last] or fc -s [pat=rep] [command]"
  115.  
  116. /* Accessors for HIST_ENTRY lists that are called HLIST. */
  117. #define histline(i) (hlist[(i)]->line)
  118. #define histdata(i) (hlist[(i)]->data)
  119.  
  120. #define FREE_RLIST() \
  121.     do { \
  122.         for (rl = rlist; rl; ) { \
  123.             REPL *r;    \
  124.             r = rl->next; \
  125.             if (rl->pat) \
  126.                 free (rl->pat); \
  127.             if (rl->rep) \
  128.                 free (rl->rep); \
  129.             free (rl); \
  130.             rl = r; \
  131.         } \
  132.     } while (0)
  133.  
  134. /* String to execute on a file that we want to edit. */
  135. #define FC_EDIT_COMMAND "${FCEDIT:-${EDITOR:-vi}}"
  136.  
  137. int
  138. fc_builtin (list)
  139.      WORD_LIST *list;
  140. {
  141.   register int i;
  142.   register char *sep;
  143.   int numbering, reverse, listing, execute;
  144.   int histbeg, histend, last_hist, retval, first, opt;
  145.   FILE *stream;
  146.   REPL *rlist = (REPL *) NULL, *rl;
  147.   char *ename = NULL, *command, *newcom, *line;
  148.   HIST_ENTRY **hlist;
  149.   char fn[MAXPATHLEN];
  150.  
  151.   numbering = 1;
  152.   reverse = listing = execute = 0;
  153.  
  154.   /* Parse out the options and set which of the two forms we're in. */
  155.  
  156.   while (list && *list->word->word == '-')
  157.     {
  158.       register char *s = &((list->word->word)[1]);
  159.  
  160.       if (!isletter (*s))
  161.     break;
  162.  
  163.       while (opt = *s++)
  164.     {
  165.       switch (opt)
  166.         {
  167.         case 'n':
  168.           numbering = 0;
  169.           break;
  170.  
  171.         case 'l':
  172.           listing = 1;
  173.           break;
  174.  
  175.         case 'r':
  176.           reverse = 1;
  177.           break;
  178.  
  179.         case 's':
  180.           execute = 1;
  181.           break;
  182.  
  183.         case 'e':
  184.           list = list->next;
  185.           if (list == NULL)
  186.         {
  187.               builtin_error (USAGE);
  188.               return (EX_USAGE);
  189.         }
  190.           ename = list->word->word;
  191.           break;
  192.  
  193.         default:
  194.           builtin_error (USAGE);
  195.           return (EX_USAGE);
  196.         }
  197.     }
  198.       list = list->next;
  199.     }
  200.  
  201.   if (ename && (*ename == '-') && (ename[1] == '\0'))
  202.     execute = 1;
  203.  
  204.   /* The "execute" form of the command (re-run, with possible string
  205.      substitutions). */
  206.   if (execute)
  207.     {
  208.       while (list && ((sep = (char *)strchr (list->word->word, '=')) != NULL))
  209.     {
  210.       *sep++ = '\0';
  211.       rl = (REPL *)xmalloc (sizeof (REPL));
  212.       rl->next = (REPL *)NULL;
  213.       rl->pat = savestring (list->word->word);
  214.       rl->rep = savestring (sep);
  215.  
  216.       if (rlist == NULL)
  217.         rlist = rl;
  218.       else
  219.         {
  220.           rl->next = rlist;
  221.           rlist = rl;
  222.         }
  223.       list = list->next;
  224.     }
  225.  
  226.       /* If we have a list of substitutions to do, then reverse it
  227.      to get the replacements in the proper order. */
  228.  
  229.       if (rlist && rlist->next)
  230.     rlist = (REPL *) reverse_list ((GENERIC_LIST *) rlist);
  231.  
  232.       hlist = history_list ();
  233.  
  234.       /* If we still have something in list, it is a command spec.
  235.      Otherwise, we use the most recent command in time. */
  236.       if (list)
  237.     command = fc_gethist (list->word->word, hlist);
  238.       else
  239.     command = fc_gethist ((char *) NULL, hlist);
  240.  
  241.       if (command == NULL)
  242.     {
  243.       builtin_error ("no command found");
  244.       if (rlist)
  245.         FREE_RLIST ();
  246.  
  247.       return (EXECUTION_FAILURE);
  248.     }
  249.  
  250.       if (rlist)
  251.     {
  252.       newcom = fc_dosubs (command, rlist);
  253.       free (command);
  254.       FREE_RLIST ();
  255.       command = newcom;
  256.     }
  257.  
  258.       printf ("%s\n", command);
  259.       fc_replhist (command);    /* replace `fc -e -' with command */
  260.       return (parse_and_execute (command, "fc", -1));
  261.     }
  262.  
  263.   /* This is the second form of the command (the list-or-edit-and-rerun
  264.      form). */
  265.   hlist = history_list ();
  266.   for (i = 0; hlist[i]; i++);
  267.  
  268.   /* With the Bash implementation of history, the current command line
  269.      ("fc blah..." and so on) is already part of the history list by
  270.      the time we get to this point.  This just skips over that command
  271.      and makes the last command that this deals with be the last command
  272.      the user entered before the fc. */
  273.  
  274.   last_hist = i - 2;
  275.  
  276.   if (list)
  277.     {
  278.       histbeg = fc_gethnum (list->word->word, hlist);
  279.       list = list->next;
  280.  
  281.       if (list)
  282.     histend = fc_gethnum (list->word->word, hlist);
  283.       else
  284.     {
  285.       if (listing)
  286.         histend = last_hist;
  287.       else
  288.         histend = histbeg;
  289.     }
  290.     }
  291.   else
  292.     {
  293.       /* The default for listing is the last 16 history items. */
  294.       if (listing)
  295.     {
  296.       histend = last_hist;
  297.       histbeg = histend - 16;
  298.     }
  299.       else
  300.     {
  301.       /* For editing, it is the last history command. */
  302.       histbeg = histend = last_hist;
  303.       }
  304.     }
  305.  
  306.   /* We print error messages for line specifications out of range. */
  307.   if ((histbeg < 0) || (histend < 0) ||
  308.       (histbeg > last_hist) || (histend > last_hist))
  309.     {
  310.       builtin_error ("history specification out of range");
  311.       return (EXECUTION_FAILURE);
  312.     }
  313.  
  314.   if (histend < histbeg)
  315.     {
  316.       int t = histend;
  317.  
  318.       histend = histbeg;
  319.       histbeg = t;
  320.       reverse = 1;
  321.     }
  322.  
  323.   if (listing)
  324.     stream = stdout;
  325.   else
  326.     {
  327.       numbering = 0;
  328.       sprintf (fn, "/tmp/bash%d", (int)time ((long *) 0) + (int)getpid ());
  329.  
  330.       stream = fopen (fn, "w");
  331.  
  332.       if (!stream)
  333.     {
  334.       builtin_error ("cannot open temp file %s", fn);
  335.       return (EXECUTION_FAILURE);
  336.     }
  337.     }
  338.  
  339.   if (!reverse)
  340.     {
  341.       for (i = histbeg; i <= histend; i++)
  342.     {
  343.       QUIT;
  344.       if (numbering)
  345.         fprintf (stream, "%d", i + history_base);
  346.       if (listing)
  347.         fprintf (stream, "\t%c", histdata (i) ? '*' : ' ');
  348.       fprintf (stream, "%s\n", histline (i));
  349.     }
  350.     }
  351.   else
  352.     {
  353.       for (i = histend; i >= histbeg; i--)
  354.     {
  355.       QUIT;
  356.       if (numbering)
  357.         fprintf (stream, "%d", i + history_base);
  358.       if (listing)
  359.         fprintf (stream, "\t%c", histdata (i) ? '*' : ' ');
  360.       fprintf (stream, "%s\n", histline (i));
  361.     }
  362.     }
  363.  
  364.   if (listing)
  365.     return (EXECUTION_SUCCESS);
  366.  
  367.   fclose (stream);
  368.  
  369.   /* Now edit the file of commands. */
  370.   if (ename)
  371.     {
  372.       command = (char *)xmalloc (strlen (ename) + strlen (fn) + 2);
  373.       sprintf (command, "%s %s", ename, fn);
  374.     }
  375.   else
  376.     {
  377.       command = (char *)xmalloc (3 + strlen (FC_EDIT_COMMAND) + strlen (fn));
  378.       sprintf (command, "%s %s", FC_EDIT_COMMAND, fn);
  379.     }
  380.   parse_and_execute (command, "fc", -1);
  381.  
  382.   /* Now reopen the file and execute the edited commands. */
  383.  
  384.   stream = fopen (fn, "r");
  385.  
  386.   if (stream == NULL)
  387.     {
  388.       builtin_error ("cannot reopen temp file %s", fn);
  389.       unlink (fn);
  390.       return (EXECUTION_FAILURE);
  391.     }
  392.  
  393.   retval = EXECUTION_SUCCESS;
  394.   first = 1;
  395.  
  396.   /* First, write the commands to the history file.  This will not happen
  397.      when we call parse_and_execute, since parse_and_execute disables
  398.      the command line history while it executes. */
  399.      
  400.   while ((line = fc_readline (stream)) != NULL)
  401.     {
  402.       if (line[0] == '\n')
  403.     {
  404.       free (line);
  405.       continue;        /* Skip blank lines. */
  406.     }
  407.  
  408.       if (first)
  409.     {
  410.       first = 0;
  411.       fc_replhist (line);
  412.     }
  413.       else
  414.     fc_addhist (line);
  415.  
  416.       free (line);
  417.     }
  418.   fclose (stream);
  419.  
  420.   /* Turn on the `v' flag while maybe_execute_file runs so the commands
  421.      will be echoed as they are read by the parser. */
  422.   begin_unwind_frame ("fc builtin");
  423.   add_unwind_protect (unlink, fn);
  424.   unwind_protect_int (echo_input_at_read);
  425.   echo_input_at_read = 1;
  426.     
  427.   retval = maybe_execute_file (fn, 0);
  428.  
  429.   run_unwind_frame ("fc builtin");
  430.  
  431.   return (retval);
  432. }
  433.  
  434. /* Return an absolute index into HLIST which corresponds to COMMAND.  If
  435.    COMMAND is a number, then it was specified in relative terms.  If it
  436.    is a string, then it is the start of a command line present in HLIST. */
  437. static int
  438. fc_gethnum (command, hlist)
  439.      char *command;
  440.      HIST_ENTRY **hlist;
  441. {
  442.   int sign = 1, n, clen;
  443.   register int i, j;
  444.   register char *s;
  445.  
  446.   /* Count history elements. */
  447.   for (i = 0; hlist[i]; i++);
  448.  
  449.   /* With the Bash implementation of history, the current command line
  450.      ("fc blah..." and so on) is already part of the history list by
  451.      the time we get to this point.  This just skips over that command
  452.      and makes the last command that this deals with be the last command
  453.      the user entered before the fc. */
  454.   i -= 2;
  455.  
  456.   /* No specification defaults to most recent command. */
  457.   if (command == NULL)
  458.     return (i);
  459.  
  460.   /* Otherwise, there is a specification.  It can be a number relative to
  461.      the current position, or an absolute history number. */
  462.   s = command;
  463.  
  464.   /* Handle possible leading minus sign. */
  465.   if (s && (*s == '-'))
  466.     {
  467.       sign = -1;
  468.       s++;
  469.     }
  470.  
  471.   if (s && digit(*s))
  472.     {
  473.       n = atoi (s);
  474.       n *= sign;
  475.  
  476.       /* Anything specified greater than the last history element that we
  477.      deal with is an error. */
  478.       if (n > i + history_base)
  479.     return (-1);
  480.  
  481.       /* If the value is negative or zero, then it is an offset from
  482.      the current history item. */
  483.       if (n < 0)
  484.     return (i + n + 1);
  485.       else if (n == 0)
  486.     return (i);
  487.       else
  488.     return (n - history_base);
  489.     }
  490.  
  491.   clen = strlen (command);
  492.   for (j = i; j >= 0; j--)
  493.     {
  494.       if (STREQN (command, histline (j), clen))
  495.     return (j);
  496.     }
  497.   return (-1);
  498. }
  499.  
  500. /* Locate the most recent history line which begins with
  501.    COMMAND in HLIST, and return a malloc()'ed copy of it. */
  502. static char *
  503. fc_gethist (command, hlist)
  504.      char *command;
  505.      HIST_ENTRY **hlist;
  506. {
  507.   int i;
  508.  
  509.   if (!hlist)
  510.     return ((char *)NULL);
  511.  
  512.   i = fc_gethnum (command, hlist);
  513.  
  514.   if (i >= 0)
  515.     return (savestring (histline (i)));
  516.   else
  517.     return ((char *)NULL);
  518. }
  519.  
  520. /* Read the edited history lines from STREAM and return them
  521.    one at a time.  This can read unlimited length lines.  The
  522.    caller should free the storage. */
  523. static char *
  524. fc_readline (stream)
  525.      FILE *stream;
  526. {
  527.   register int c;
  528.   int line_len = 0, lindex = 0;
  529.   char *line = (char *)NULL;
  530.  
  531.   while ((c = getc (stream)) != EOF)
  532.     {
  533.       if ((lindex + 2) >= line_len)
  534.     line = (char *) xrealloc (line, (line_len += 128));
  535.  
  536.       if (c == '\n')
  537.     {
  538.       line[lindex++] = '\n';
  539.       line[lindex++] = '\0';
  540.       return (line);
  541.     }
  542.       else
  543.     line[lindex++] = c;
  544.     }
  545.  
  546.   if (!lindex)
  547.     {
  548.       if (line)
  549.     free (line);
  550.  
  551.       return ((char *)NULL);
  552.     }
  553.  
  554.   if (lindex + 2 >= line_len)
  555.     line = (char *)xrealloc (line, lindex + 3);
  556.  
  557.   line[lindex++] = '\n';        /* Finish with newline if none in file */
  558.   line[lindex++] = '\0';
  559.   return (line);
  560. }
  561.  
  562. /* Perform the SUBS on COMMAND.
  563.    SUBS is a list of substitutions, and COMMAND is a simple string.
  564.    Return a pointer to a malloc'ed string which contains the substituted
  565.    command. */
  566. static char *
  567. fc_dosubs (command, subs)
  568.      char *command;
  569.      REPL *subs;
  570. {
  571.   register char *new = savestring (command);
  572.   register REPL *r;
  573.  
  574.   for (r = subs; r; r = r->next)
  575.     {
  576.       register char *t;
  577.  
  578.       t = fc_replace (r->pat, r->rep, new);
  579.       free (new);
  580.       new = t;
  581.     }
  582.   return (new);
  583. }
  584.  
  585. /* Replace the occurrences of PAT with REP in COMMAND.
  586.    This returns a new string; the caller should free it. */
  587. static char *
  588. fc_replace (pat, rep, command)
  589.      char *pat, *rep, *command;
  590. {
  591.   register int i;
  592.   int patlen, replen, templen;
  593.   char *new, *temp;
  594.  
  595.   patlen = strlen (pat);
  596.   replen = strlen (rep);
  597.  
  598.   temp = savestring (command);
  599.   templen = strlen (temp);
  600.   i = 0;
  601.  
  602.   for (; (i + patlen) <= templen; i++)
  603.     {
  604.       if (STREQN (temp + i, pat, patlen))
  605.     {
  606.       new = (char *) xmalloc (1 + (replen - patlen) + templen);
  607.  
  608.       strncpy (new, temp, i);
  609.       strncpy (new + i, rep, replen);
  610.       strncpy (new + i + replen,
  611.            temp + i + patlen, templen - (i + patlen));
  612.       new[templen + (replen - patlen)] = '\0'; /* just in case */
  613.  
  614.       free (temp);
  615.       temp = new;
  616.       i += replen;
  617.       templen = strlen (temp);
  618.     }
  619.     }
  620.   return (temp);
  621. }
  622.  
  623. /* Use `command' to replace the last entry in the history list, which,
  624.    by this time, is `fc blah...'.  The intent is that the new command
  625.    become the history entry, and that `fc' should never appear in the
  626.    history list.  This way you can do `r' to your heart's content. */
  627. static void
  628. fc_replhist (command)
  629.      char *command;
  630. {
  631.   register int i;
  632.   HIST_ENTRY **hlist, *histent, *discard;
  633.   char *data;
  634.   int n;
  635.  
  636.   if (!command || !*command)
  637.     return;
  638.  
  639.   hlist = history_list ();
  640.  
  641.   if (hlist == NULL)
  642.     return;
  643.  
  644.   for (i = 0; hlist[i]; i++);
  645.   i--;
  646.  
  647.   /* History_get () takes a parameter that should be
  648.      offset by history_base. */
  649.  
  650.   histent = history_get (history_base + i);    /* Don't free this */
  651.   if (histent == NULL)
  652.     return;
  653.  
  654.   n = strlen (command);
  655.  
  656.   if (command[n - 1] == '\n')
  657.     command[n - 1] = '\0';
  658.  
  659.   if (command && *command)
  660.     {
  661.       discard = remove_history (i);
  662.       if (discard)
  663.     {
  664.       if (discard->line)
  665.         free (discard->line);
  666.       free ((char *) discard);
  667.     }
  668.       maybe_add_history (command);    /* Obeys HISTCONTROL setting. */
  669.     }
  670. }
  671.  
  672. /* Add LINE to the history, after removing a single trailing newline. */
  673. static void
  674. fc_addhist (line)
  675.      char *line;
  676. {
  677.   register int n;
  678.  
  679.   n = strlen (line);
  680.  
  681.   if (line[n - 1] == '\n')
  682.     line[n - 1] = '\0';
  683.  
  684.   if (line && *line)
  685.     maybe_add_history (line);
  686. }
  687. #endif /* HISTORY */
  688.